package com.zl.tby.screen.controller;

import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyAnimalPlantQuery;
import com.zl.tby.model.TbyAnimalPlant;
import com.zl.tby.screen.service.ITbyAnimalPlantScreenService;
import com.zl.tby.screen.vo.animalPlant.TbyAnimalPlantScreenBean;
import com.zl.tby.screen.vo.animalPlant.TbyAnimalPlantScreenQuery;
import com.zl.tby.service.ITbyAnimalPlantService;
import com.zl.tby.vo.bean.SpeciesLegendBean;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 野生动植物-大屏-控制器
 * @author sunjiaxing
 * @version 1.0
 * @since 2024/8/16 22:37
 */
@Valid
@Controller
@Mapping("/animal_plant_screen")
public class TbyAnimalPlantScreenController {

    @Inject
    private ITbyAnimalPlantScreenService tbyAnimalPlantScreenService;
    @Inject
    private ITbyAnimalPlantService tbyAnimalPlantService;

    /**
     * 野生物种资源
     * @return 结果
     */
    @Mapping(value = "/get_type_level", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getTypeLevel(Long typeLevel,String protectionLevel) {
        List<TbyAnimalPlantScreenBean> list = this.tbyAnimalPlantScreenService.getTypeLevel(typeLevel,protectionLevel);
        return R.ok(list);
    }

    /**
     * 野生物种资源统计图
     * @param typeLevel: 物种分类大类
     * @return 结果
     */
    @Mapping(value = "/group_by_category", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> groupByCategory(Long typeLevel) {
        List<TbyAnimalPlantScreenBean> list = this.tbyAnimalPlantScreenService.groupByCategory(typeLevel);
        return R.ok(list);
    }

    /**
     * 野生物种资源
     * @return 结果
     */
    @Mapping(value = "/get_animal_type_level", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getAnimalTypeLevel(Long typeLevel,String protectionLevel) {
        List<TbyAnimalPlantScreenBean> list = this.tbyAnimalPlantScreenService.getAnimalTypeLevel(typeLevel,protectionLevel);
        return R.ok(list);
    }

    /**
     * 野生物种资源
     * @return 结果
     */
    @Mapping(value = "/get_plant_type_level", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getPlantTypeLevel(Long typeLevel,String protectionLevel) {
        List<TbyAnimalPlantScreenBean> list = this.tbyAnimalPlantScreenService.getPlantTypeLevel(typeLevel,protectionLevel);
        return R.ok(list);
    }

    /**
     * 旗舰物种查询
     * @return 结果
     */
    @Mapping(value = "/get_species", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getSpecies(Long typeLevel) {
        List<TbyAnimalPlantScreenBean> list = this.tbyAnimalPlantScreenService.getSpecies(typeLevel);
        return R.ok(list);
    }

    /**
     * 保护动物查询
     * @return 结果
     */
    @Mapping(value = "/get_protection", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getProtection() {
        String[] array = {"一级", "二级", "三有", "省重点"};
        String[] levelArray = {"level_1", "level_2", "level_5", "level_6"};
        String[] otherArray = {"iucn", "cites"};
        Long topId = 1L;
        return R.ok(this.tbyAnimalPlantScreenService.getProtection(array, levelArray, otherArray, topId));
    }

    /**
     * 保护动物查询
     * @return 结果
     */
    @Mapping(value = "/get_protection_by_plant", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getProtectionByPlant() {
        String[] array = {"一级", "二级", "省重点"};
        String[] levelArray = {"level_1", "level_2", "level_6"};
        String[] otherArray = {"iucn", "cites"};
        Long topId = 2L;
        return R.ok(this.tbyAnimalPlantScreenService.getProtection(array, levelArray, otherArray, topId));
    }

    /**
     * 保护动物查询
     * @return 结果
     */
    @Mapping(value = "/get_protection_by_gungi", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getProtectionByGungi() {
        String[] array = {"中国珍稀濒危真菌物种"};
        String[] levelArray = {"cherish"};
        String[] otherArray = {"iucn"};
        Long topId = 13L;
        List<TbyAnimalPlantScreenBean> list = new ArrayList<>();
        for (int i = 0; i < otherArray.length; i++) {
            TbyAnimalPlantScreenBean dto = new TbyAnimalPlantScreenBean();
            String val = otherArray[i];
            dto.setValue(tbyAnimalPlantScreenService.searchTotalCountByProp(val, topId));
            dto.setName("IUCN");
            dto.setProtectionLevel(val);
            list.add(dto);
        }
        for (int i = 0; i < array.length; i++) {
            TbyAnimalPlantScreenBean dto = new TbyAnimalPlantScreenBean();
            String val = levelArray[i];
            dto.setValue(tbyAnimalPlantScreenService.searchTotalCountByCherish(1, topId));
            dto.setName(array[i]);
            dto.setProtectionLevel(val);
            list.add(dto);
        }
        return R.ok(list);
    }

    /**
     * 生态学接口
     * @return 结果
     */
    @Mapping(value = "/get_gungi_ecology", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getGungiEcology() {
        String[] ecologyArray = {"1", "2", "3"};
        String[] valueArray = {"腐生型", "共生型", "寄生型"};
        List<TbyAnimalPlantScreenBean> list = new ArrayList<>();
        for (int i = 0; i < ecologyArray.length; i++) {
            TbyAnimalPlantScreenBean dto = new TbyAnimalPlantScreenBean();
            String val = ecologyArray[i];
            dto.setValue(tbyAnimalPlantScreenService.searchTotalCountByEcology(val));
            dto.setName(valueArray[i]);
            dto.setProtectionLevel(val);
            list.add(dto);
        }
        return R.ok(list);
    }

    /**
     * 特色物种查询
     * @return 结果
     */
    @Mapping(value = "/gettswz", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getTSWZ(TbyAnimalPlantScreenQuery dto) {
        List<TbyAnimalPlantScreenBean> list = this.tbyAnimalPlantScreenService.getTSWZ(dto);
        return R.ok(list);
    }

    /**
     * 特色物种查询
     * @return 结果
     */
    @Mapping(value = "/get_change_trend", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getChangeTrend(TbyAnimalPlantScreenQuery dto) throws ParseException {
        List<TbyAnimalPlantScreenBean> list = this.tbyAnimalPlantScreenService.getChangeTrend(dto);
        return R.ok(list);
    }

    /**
     * 特色物种查询new2025年10月17日 09:16:55bjs
     * @return 结果
     */
    @Mapping(value = "/get_change_trend_new", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getChangeTrendNew(TbyAnimalPlantScreenQuery dto) throws ParseException {
        List<TbyAnimalPlantScreenBean> list = this.tbyAnimalPlantScreenService.getChangeTrendNew(dto);
        return R.ok(list);
    }

    @Mapping(value = "/get_fungi", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getFungi(TbyAnimalPlantScreenQuery dto) throws ParseException {
        List<TbyAnimalPlantScreenBean> list = this.tbyAnimalPlantScreenService.getfungi(dto);
        return R.ok(list);
    }

    /**
     * 获取图片
     * @return 结果
     */
    @Mapping(value = "/getview", method = MethodType.GET)
    public R<List<TbyAnimalPlantScreenBean>> getview(Long id) {
        List<TbyAnimalPlantScreenBean> list = this.tbyAnimalPlantScreenService.getview(id);
        return R.ok(list);
    }

    /**
     * 获取物种图例
     * @param speciesParentId：物种大类
     * @return 物种图例
     */
    @Mapping(value = "/get_legend", method = MethodType.GET)
    public R<List<SpeciesLegendBean>> getLegend(Long speciesParentId) {
        return R.ok(this.tbyAnimalPlantScreenService.getLegendByParentId(speciesParentId));
    }

    /**
     * 获取冠名物种
     * @param limit: 获取数量
     * @return 数据
     */
    @Mapping(value = "/sponsorship_species", method = MethodType.GET)
    public R<List<Map<String, Object>>> sponsorshipSpecies(Integer limit) {
        TbyAnimalPlantQuery query = new TbyAnimalPlantQuery();
        query.setSponsorship(1);
        List<TbyAnimalPlant> dataList = this.tbyAnimalPlantService.list(query);
        List<Map<String, Object>> rsList = new ArrayList<>();
        int start = 0;
        for (TbyAnimalPlant tbyAnimalPlant : dataList) {
            Map<String, Object> map = new HashMap<>(2);
            map.put("id", tbyAnimalPlant.getId());
            map.put("name", tbyAnimalPlant.getChName());
            rsList.add(map);
            start++;
            if (start >= limit) {
                break;
            }
        }
        return R.ok(rsList);
    }
}
